#include <Ewl.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "callbacks.h"
#include "gui.h"

#define HDEF 640
#define WDEF 320

#ifdef DEBUG
#define PRINTFUNC(x) printf("DEBUG: %s\n",x);
#else
#define PRINTFUNC(x)
#endif

Ewl_Widget *win = NULL;
Ewl_Widget *tb = NULL;
Ewl_Widget *sb = NULL;
buffer_t *fbuffer = NULL;

int gui_init(int argc, char **argv)
{
    PRINTFUNC(__PRETTY_FUNCTION__);
    /*  Ewl_Widget *win moved to be global */
    Ewl_Widget *box = NULL;
    Ewl_Widget *label = NULL;
    Ewl_Widget *o = NULL;
    Ewl_Widget *h_menubar = NULL;
    Ewl_Widget *scrollpane = NULL;
    Ewl_Widget *foo = NULL;

    /*Init the Library*/
    if(!ewl_init(&argc, argv))
    {
        printf("Unable to init ewl\n");
        return 1;
    }

    /* "Create" new window object and assign it to our widget */
    win = ewl_window_new();
    /* Window Properties */
    ewl_window_title_set(EWL_WINDOW(win), "eedit");
    ewl_window_name_set(EWL_WINDOW(win), "eedit_main_window");
    ewl_window_class_set(EWL_WINDOW(win), "EWLWindow");
    ewl_object_fill_policy_set(EWL_OBJECT(win), EWL_FLAG_FILL_ALL);
    ewl_object_size_request(EWL_OBJECT(win), HDEF, WDEF);
    /* Program stuff */
    ewl_callback_append(win, EWL_CALLBACK_DELETE_WINDOW, run_dialog, NULL);
    ewl_widget_show(win);


    /* Create the container */
    box = ewl_vbox_new();
    /* Add the box to the window */
    ewl_container_child_append(EWL_CONTAINER(win), box);
    ewl_object_fill_policy_set(EWL_OBJECT(box), EWL_FLAG_FILL_ALL);
    ewl_widget_show(box);

    /* Build the Menu bar */
    h_menubar = ewl_hmenubar_new();
    {
        int i;
        Ewl_Widget *item;
        Ewl_Widget *foo;
        Ewl_Widget *sub;
        Ewl_Widget *sep;
        /* File Menu */
        item = ewl_menu_new();
        ewl_button_label_set(EWL_BUTTON(item), "File");
        ewl_container_child_append(EWL_CONTAINER(h_menubar), item);
        ewl_object_fill_policy_set(EWL_OBJECT(item), EWL_FLAG_FILL_SHRINK);
        ewl_widget_show(item);

        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "New  CTRL + n");
        ewl_button_image_set(EWL_BUTTON(foo),EAPPS_DATA_DIR "/pixmaps/filenew.png",NULL);
        ewl_container_child_append(EWL_CONTAINER(item), foo);
        ewl_widget_show(foo);

        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "Open  CTRL + o");
        ewl_container_child_append(EWL_CONTAINER(item), foo);
        ewl_widget_show(foo);

        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "Save  CTRL + s");
        ewl_button_image_set(EWL_BUTTON(foo),EAPPS_DATA_DIR "/pixmaps/filesave.png",NULL);
        ewl_container_child_append(EWL_CONTAINER(item), foo);
        ewl_widget_show(foo);

        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "Close  CTRL + Q");
        ewl_button_image_set(EWL_BUTTON(foo),EAPPS_DATA_DIR "/pixmaps/fileclose.png",NULL);
        ewl_container_child_append(EWL_CONTAINER(item), foo);
        ewl_widget_show(foo);

        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "Quit Application");
        ewl_callback_append(foo, EWL_CALLBACK_CLICKED, run_dialog, NULL);
        ewl_button_image_set(EWL_BUTTON(foo),EAPPS_DATA_DIR "/pixmaps/exit.png",NULL);
        ewl_container_child_append(EWL_CONTAINER(item), foo);
        ewl_widget_show(foo);
#if 1
        /* Seperator */
        sep = ewl_vseparator_new();
        ewl_container_child_append(EWL_CONTAINER(h_menubar), sep);
        ewl_widget_show(sep);
#endif
        /* Options Menu */
        item = ewl_menu_new();
        ewl_object_fill_policy_set(EWL_OBJECT(item), EWL_FLAG_FILL_SHRINK);
        ewl_button_label_set(EWL_BUTTON(item), "Options");
        ewl_container_child_append(EWL_CONTAINER(h_menubar), item);
        ewl_widget_show(item);

        sub = ewl_menu_new();
        ewl_button_label_set(EWL_BUTTON(sub), "Syntax Highlighting");
        ewl_container_child_append(EWL_CONTAINER(item), sub);
        ewl_widget_show(sub);

        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "C / C++");
        ewl_container_child_append(EWL_CONTAINER(sub), foo);
        ewl_widget_show(foo);

        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "Java");
        ewl_container_child_append(EWL_CONTAINER(sub), foo);
        ewl_widget_show(foo);

        /* Seperator */
        sep = ewl_vseparator_new();
        ewl_container_child_append(EWL_CONTAINER(h_menubar), sep);
        ewl_widget_show(sep);

        item = ewl_menu_new();
        ewl_button_label_set(EWL_BUTTON(item), "Help");
        ewl_button_image_set(EWL_BUTTON(item),EAPPS_DATA_DIR "/pixmaps/edit_add.png",NULL);
        ewl_object_fill_policy_set(EWL_OBJECT(item), EWL_FLAG_FILL_SHRINK);
        ewl_container_child_append(EWL_CONTAINER(h_menubar), item);
        ewl_widget_show(item);

        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "About");
        ewl_button_image_set(EWL_BUTTON(foo),EAPPS_DATA_DIR "/pixmaps/help.png",NULL);
        ewl_container_child_append(EWL_CONTAINER(item), foo);
        ewl_widget_show(foo);

        sep = ewl_vseparator_new();
        ewl_container_child_append(EWL_CONTAINER(h_menubar), sep);
        ewl_widget_show(sep);

        item = ewl_spacer_new();
        ewl_object_fill_policy_set(EWL_OBJECT(item), EWL_FLAG_FILL_ALL);
        ewl_container_child_append(EWL_CONTAINER(h_menubar), item);
        ewl_widget_show(item);

        /*Show Menu*/
        ewl_container_child_append(EWL_CONTAINER(box), h_menubar);
        ewl_object_fill_policy_set(EWL_OBJECT(item), EWL_FLAG_FILL_SHRINK);
        ewl_object_alignment_set(EWL_OBJECT(h_menubar), EWL_FLAG_ALIGN_LEFT);
        ewl_widget_show(h_menubar);
    }

    /* Create a label */
    label = ewl_text_new();
    /* Add the label to the box */
    ewl_container_child_append(EWL_CONTAINER(box), label);
    ewl_object_fill_policy_set(EWL_OBJECT(label), EWL_FLAG_FILL_NONE);
    ewl_text_styles_set(EWL_TEXT(label), EWL_TEXT_STYLE_DOUBLE_UNDERLINE
                        | EWL_TEXT_STYLE_OUTLINE
                        | EWL_TEXT_STYLE_SOFT_SHADOW);
    ewl_text_double_underline_color_set(EWL_TEXT(label), 50, 50, 50, 255);
    ewl_text_shadow_color_set(EWL_TEXT(label), 128, 128, 128, 128);
    ewl_text_outline_color_set(EWL_TEXT(label), 200, 200, 200, 200);

    ewl_text_text_append(EWL_TEXT(label), "Enlightened Editing");
    ewl_widget_show(label);

    scrollpane = ewl_scrollpane_new();
#if 0

    ewl_scrollpane_hscrollbar_flag_set(EWL_SCROLLPANE(scrollpane),
                                       EWL_SCROLLPANE_FLAG_ALWAYS_HIDDEN);
#endif

    ewl_container_child_append(EWL_CONTAINER(box), scrollpane);
    ewl_widget_name_set(scrollpane, "scrollpane");
    ewl_widget_show(scrollpane);

    /* Create Entry*/
    tb = ewl_entry_new();
    ewl_callback_del(EWL_WIDGET(tb),EWL_CALLBACK_KEY_DOWN,
                     ewl_entry_cb_key_down);

    ewl_callback_append(EWL_WIDGET(tb),EWL_CALLBACK_KEY_DOWN,
                        eedit_key_down_cb,NULL);

    ewl_container_child_append(EWL_CONTAINER(scrollpane), tb);
    ewl_object_fill_policy_set(EWL_OBJECT(tb), EWL_FLAG_FILL_NONE);
    ewl_widget_name_set(tb,"entry");
    ewl_entry_multiline_set(EWL_ENTRY(tb), TRUE);

    ewl_text_wrap_set(EWL_TEXT(tb), EWL_TEXT_WRAP_WORD);
    ewl_text_font_set(EWL_TEXT(tb), "ewl/monospace");
    ewl_text_font_size_set(EWL_TEXT(tb), 12);
    ewl_text_underline_color_set(EWL_TEXT(tb), 0, 0, 0, 255);
    ewl_text_double_underline_color_set(EWL_TEXT(tb), 50, 50, 50, 255);
    ewl_text_shadow_color_set(EWL_TEXT(tb), 128, 128, 128, 128);
    ewl_text_outline_color_set(EWL_TEXT(tb), 200, 200, 200, 200);
    ewl_text_strikethrough_color_set(EWL_TEXT(tb), 0, 0, 0, 255);
    ewl_text_glow_color_set(EWL_TEXT(tb), 0, 255, 0, 128);
    ewl_text_selectable_set(EWL_TEXT(tb), 1);

    /* Create Statusbar */
    sb = ewl_statusbar_new();
    ewl_container_child_append(EWL_CONTAINER(box), sb);
    ewl_statusbar_push(EWL_STATUSBAR(sb), "Line: 1  Cursor: 1  Last: 0");
    ewl_widget_name_set(sb,"statusbar");
    ewl_widget_show(sb);
    ewl_widget_show(tb);

    return 0;
}

int gui_update_text(buffer_t* buffer)
{
    /* Assertions */
    assert(buffer!=NULL);

    /* Safe Buffer */
    int i;
    char *print_buffer = calloc(CHUNK_SIZE+2,sizeof(char));

    fbuffer = buffer;

    chunk_t* chunk = buffer->head;
    assert(chunk!=NULL);
    assert(chunk->mem!=NULL);

    ewl_text_clear(EWL_TEXT(tb));

    snprintf(print_buffer,
            CHUNK_SIZE,"Buffer Currently Has [%i] Chunks",buffer->num_chunks);

    DEBUG_INFO(print_buffer);

    while(chunk!=NULL)
    {
        snprintf(print_buffer,CHUNK_SIZE,"%s",chunk->mem);
        ewl_text_text_append(EWL_TEXT(tb),print_buffer);
        chunk=chunk->next;
    }
    ewl_text_cursor_position_set(EWL_TEXT(tb),0);

    ewl_text_font_apply(EWL_TEXT(tb),
                        "ewl/monospace", ewl_text_length_get(EWL_TEXT(tb)));
    return 0;
}

int gui_update_status(char *status)
{
    ewl_statusbar_push(EWL_STATUSBAR(sb), status);

    return 0;
}

buffer_t* gui_get_buffer_for_widget(Ewl_Widget *w)
{
    /* Look up through our gui text panes */
    return fbuffer;
}
